package com.ss.android.article.base.feature.detail.view;

import android.graphics.Bitmap;
import android.webkit.WebView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.feature.app.browser.c;
import com.ss.android.article.base.feature.app.e.TTAndroidObject;
import com.ss.android.article.base.feature.detail2.ArticleDetailWebPage;
import com.ss.android.article.browser.R;

import java.lang.ref.WeakReference;

public class MyWebViewClient extends c {
    protected final WeakReference<ArticleDetailWebPage> mArticleDetailWebPageRef;
    protected volatile String e;

    public MyWebViewClient(ArticleDetailWebPage articleDetailWebPage) {
        this.mArticleDetailWebPageRef = new WeakReference(articleDetailWebPage);
    }

    public final void a(String arg1) {
        this.e = arg1;
    }

    public void doUpdateVisitedHistory(WebView webView, String url, boolean isReload) {
        super.doUpdateVisitedHistory(webView, url, isReload);
        if (Logger.debug()) {
            Logger.v("MyWebViewClient", "doUpdateVisitedHistory " + url + " " + isReload);
        }

        String originalUrl = webView.getOriginalUrl();
        if (originalUrl != null && (originalUrl.equals(webView.getTag(R.id.webview_clear_history_key)))) {
            if ((webView.canGoBack()) || (webView.canGoForward())) {
                webView.clearHistory();
            }
            webView.setTag(R.id.webview_clear_history_key, null);
        }

        ArticleDetailWebPage articleDetailWebPage = this.mArticleDetailWebPageRef.get();
        if (articleDetailWebPage != null) {
            webView.getTag(R.id.webview_history_key);
            articleDetailWebPage.doUpdateVisitedHistory(webView, url);
        }
        webView.setTag(R.id.webview_history_key, Boolean.TRUE);
    }

    public void onLoadResource(WebView webView, String url) {
        if (Logger.debug() && !TTUtils.isValidUrl(url)) {
            Logger.d("MyWebViewClient", "onLoadResource " + url);
        }

        ArticleDetailWebPage articleDetailWebPage = this.mArticleDetailWebPageRef.get();
        TTAndroidObject ttAndroidObject = articleDetailWebPage != null ? articleDetailWebPage.getTTAndroidObject() : null;
        if (ttAndroidObject != null) {
            try {
                ttAndroidObject.parseMsgQueue(url);
            } catch (Exception exception) {
            }
        }
    }

    public void onPageFinished(WebView webView, String url) {
        super.onPageFinished(webView, url);
        ArticleDetailWebPage articleDetailWebPage = this.mArticleDetailWebPageRef.get();
        if (articleDetailWebPage != null) {
            articleDetailWebPage.onPageFinished(webView, url);
        }
    }

    public void onPageStarted(WebView webView, String url, Bitmap favicon) {
        super.onPageStarted(webView, url, favicon);
        ArticleDetailWebPage articleDetailWebPage = this.mArticleDetailWebPageRef.get();
        if (articleDetailWebPage != null) {
            articleDetailWebPage.onPageStarted(webView, url);
        }
    }

    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
        super.onReceivedError(webView, errorCode, description, failingUrl);
        ArticleDetailWebPage articleDetailWebPage = this.mArticleDetailWebPageRef.get();
        if (articleDetailWebPage != null) {
            articleDetailWebPage.onReceivedError(errorCode, description);
        }
    }

    public void onScaleChanged(WebView webView, float oldScale, float newScale) {
        super.onScaleChanged(webView, oldScale, newScale);
    }

    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        ArticleDetailWebPage articleDetailWebPage = this.mArticleDetailWebPageRef.get();
        boolean v0_1 = articleDetailWebPage != null ? articleDetailWebPage.shouldOverrideUrlLoading(webView, url) : false;
        return v0_1;
    }
}

